package com.project.server;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * servlet工厂类
 */
public class ServletFactory {

    private static Properties pro = new Properties();

    static{
        try {
            pro.load(new FileReader("web.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Servlet getServlet(String url){

        //根据url，得到文件中的值
        String classPath = pro.getProperty(url);

        if (classPath == null){//路径为空证明他请求的是文件
            return null;
        }

        try {
            Class servletClass = Class.forName(classPath);
            Object obj = servletClass.getConstructor().newInstance();

            return (Servlet) obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
